var p=1;
var url = "sky.php";
$(function() {
	find(1,100,1);
});
function find(page,rows,flag){
	$("#dialog").hide();
	p = page;
	if(flag==2){		
		p=p+1;
		page=p;
	}
	if(flag==3){		
		p=p-1;
		if(p<=0)
			p=1;
		page=p;
	}
	$.post(url,{doAction:'act_zhsj',page:page,rows:rows},function(data){	
		$('#name').html(data.name);
		$('#pno').html(data.pno);
		$('#tc').html(data.tc);
		$('#dquyu').html(data.dquyu);
		$('#tcs').html(data.tcs);
		$('#tcs1').html(data.tcs);	
		$.ajaxSetup({   
	        async : false  
	    });		
		$("#imgList  tr:not(:first)").empty("");
		 $.each(data.r,function(index,item){
			  if(item.sprice!=0){
				  if(item.state=="已购买"){
					   $("#imgList").append(  
		                       "<tr > <td >"+item.tcname+"</td>"
		                       +"<td>" + item.sprice + "</td>"  
		                     + "<td>" +  0  + "</td>"  
		                        +"<td >"+item.state+"</td>"
		                     + "</tr>");  
				 }
				  else if(item.state=="未开放"){
					   $("#imgList").append(  
		                       "<tr > <td >"+item.tcname+"</td>"
		                       +"<td>" + item.sprice + "</td>"  
		                     + "<td>" +  item.upprice  + "</td>"  
		                        +"<td class='f_red'>"+item.state+"</td>"
		                     + "</tr>");   
				 }
				  else{
					   $("#imgList").append(  
		                       "<tr > <td >"+item.tcname+"</td>"
		                       +"<td>" + item.sprice + "</td>"  
		                     + "<td>" +  item.upprice  + "</td>"  
		                        +"<td class='f_red' id='"+item.sprice+"'><a href='#' onclick='showp1(\""+item.tcname+"\","+item.upprice+","+item.id+","+item.sprice+")'>"+item.state+"</a></td>"
		                     + "</tr>");   
				 }
			  } 
			
          
           });  

	},"json");
}
function showp1(tcs,price,id,sprice) {
	if(confirm("是否重新填写商务中心?")){
		find(1,100,1);
		$("#"+sprice).html('<li><input name="swzx" id="swzx" type="text" class="w_80" maxlength="20" placeholder="请输入商务中心ID" onblur="chechBus(this.value)" onfocus="clearSpan(\'swname\')"/>&nbsp;&nbsp;<span id="swname" style="color: red;"></span>&nbsp; &nbsp; <a href="#" onclick="showp2(\''+tcs+'\','+price+','+id+')">确定</a></li>')
	}else{
		 showp(tcs,price,id,"");
	}
}
function chechBus(pno){
	$.ajaxSetup({   
        async : false  
    });
	$.post(url,{doAction:"act_chechname",tjr:pno},function(data){
		var slevel = data.slevel;
		 var flag = data.flag;
		 var str = "";
		 if (slevel != "") {
			str = slevel;
		}else {
			str = flag;
		}
		 $("#swname").html(str);
	},"json");
	
}
function clearSpan(val){
	var value = $("#"+val).html();	
	$("#"+val).html('');	 
}
function showp2(tcs,price,id) {
	if($("#swzx").val()==''){
		alert("请输入商务中心!");
		return;
	}
	$.ajaxSetup({   
        async : false  
    });
	$.post(url,{doAction:"act_chechname",tjr:$("#swzx").val()},function(data){
		var slevel = data.slevel;
		 var flag = data.flag;
		 if(data.sign=='1'){
			 showp(tcs,price,id,$("#swzx").val());
		 }else{
			 if (slevel != "") {
				alert(slevel);
			}else {
				alert(flag);
			}		
		 }
	},"json");
	
}
function showp(tcs,price,id,dquyu){	
	var dquyu1 =dquyu;
	if(dquyu==""){
		dquyu1=$('#dquyu').html();
	}
	if(confirm("是否升级?升级后将扣除"+price+"积分,商务中心为"+dquyu1+",并按照升级后的星级比例兑换电子积分。")){
		$("#dialog").hide();	
		$.ajax({
			 type:'POST',
			 url: url, 
			 data: {doAction: "act_mkcheck",id:id,upprice:price,tcname:encodeURIComponent(tcs),dquyu:dquyu},
			 async: false,
			 dataType: 'json',
			 success: function(data){			
				 	if(data.flag==1){				
						$('#id').val(id);
						$('#tcs').html(tcs);
						$('#tcs1').html(tcs);						
						$('#price').html(price);
						find(1,100,1);
						$("#dialog").show();
					}else if(data.flag==0){
						alert("状态异常，无法进行该操作!");
					}else if(data.flag==2){
						alert(data.msg);
					}
			},
			 error: function(err){		
				alert('网络故障');
			}
		});      
	}
	
}
function toMk(){
	$("#dialog").hide();	
}